<?php
include "../sign.php";
class LemonApi
{
   

    /**
     * 扫码支付接口
     * @return string
     */
    public function Pay()
    {  
        
        $pay_ment = array_key_exists("pay_ment", $_POST) ? $_POST['pay_ment'] : "";

        switch ($pay_ment) {
            case 'scan':
               return $this->scanPay();
                break;
            case 'H5':
              return $this->h5Pay();
              
                break;
            case 'B2C':
               return $this->ebank();
               
                break;
            // case 'barcode':
            //    return $this->barcode();
               
            //     break;
            
            default:
                return "参数错误";
                break;
        }   

    }

    public function h5Pay()
    {
        $urlString = "http://13.67.73.179/v1/api/h5/pay";

        $interfaceVersion = array_key_exists("interface_version", $_POST) ? $_POST['interface_version'] : "";
        $key = array_key_exists("key", $_POST) ? $_POST['key'] : "";
        $merchantCode = array_key_exists("merchant_code", $_POST) ? $_POST['merchant_code'] : "";
        $amount = array_key_exists("amount", $_POST) ? $_POST['amount'] : "";
        $clientIp = array_key_exists("client_ip", $_POST) ? $_POST['client_ip'] : "";
        $notifyUrl = array_key_exists("notify_url", $_POST) ? $_POST['notify_url'] : "";
        $orderId = array_key_exists("order_id", $_POST) ? $_POST['order_id'] : "";
        $productDesc = array_key_exists("product_desc", $_POST) ? $_POST['product_desc'] : "";
        $productExt = array_key_exists("product_ext", $_POST) ? $_POST['product_ext'] : "";
        $productName = array_key_exists("product_name", $_POST) ? $_POST['product_name'] : "";
        $serviceType = array_key_exists("service_type", $_POST) ? $_POST['service_type'] : "";

        if ($amount == "" || $clientIp == "" || $notifyUrl == "" || $orderId == "" || $productName == "" || $serviceType == "")
            return '{"result":"0","message":"参数错误"}';

        $dataArray = [];
        $dataArray ["amount"] = $amount;
        $dataArray["clientIp"] = $clientIp;
        $dataArray["interfaceVersion"] = $this->interfaceVersion;
        $dataArray["merchantCode"] = $this->merchantCode;
        $dataArray["notifyUrl"] = $notifyUrl;
        $dataArray["orderId"] = $orderId;

        if ($productDesc != "")
            $dataArray["productDesc"] = $productDesc;
        if ($productExt != "")
            $dataArray["productExt"] = $productExt;
        $dataArray["productName"] = $productName;
        $dataArray["serviceType"] = $serviceType;
        //生成签名
        $sign = Sign::productSign($dataArray, $this->key);
        $dataArray["sign"] = $sign;

        $result = json_decode($this->postSend($urlString, $dataArray));


        if ($result->status == "SUCCESS")
            return '{"result":"1","message":"' . $result->payUrl . '"}';
        else
            return '{"result":"0","message":"' . $result->message . '"}';

    }

    public function scanPay()
    {
        $urlString = "http://13.67.73.179/v1/api/scancode/pay";
        $interfaceVersion = array_key_exists("interface_version", $_POST) ? $_POST['interface_version'] : "";
        $key = array_key_exists("key", $_POST) ? $_POST['key'] : "";
        $merchantCode = array_key_exists("merchant_code", $_POST) ? $_POST['merchant_code'] : "";
        $amount = array_key_exists("amount", $_POST) ? $_POST['amount'] : "";
        $clientIp = array_key_exists("client_ip", $_POST) ? $_POST['client_ip'] : "";
        $notifyUrl = array_key_exists("notify_url", $_POST) ? $_POST['notify_url'] : "";
        $orderId = array_key_exists("order_id", $_POST) ? $_POST['order_id'] : "";
        $productDesc = array_key_exists("product_desc", $_POST) ? $_POST['product_desc'] : "";
        $productExt = array_key_exists("product_ext", $_POST) ? $_POST['product_ext'] : "";
        $productName = array_key_exists("product_name", $_POST) ? $_POST['product_name'] : "";
        $serviceType = array_key_exists("service_type", $_POST) ? $_POST['service_type'] : "";
        //var_dump($amount,$clientIp,$notifyUrl,$orderId,$productName,$serviceType);
        if ($amount == "" || $clientIp == "" || $notifyUrl == "" || $orderId == "" || $productName == "" || $serviceType == "")
            return '{"result":"0","message":"参数错误"}';

        $dataArray = [];
        $dataArray ["amount"] = $amount;
        $dataArray["clientIp"] = $clientIp;
        $dataArray["interfaceVersion"] = $this->interfaceVersion;
        $dataArray["merchantCode"] = $this->merchantCode;
        $dataArray["notifyUrl"] = $notifyUrl;
        $dataArray["orderId"] = $orderId;
        if ($productDesc != "")
            $dataArray["productDesc"] = $productDesc;
        if ($productExt != "")
            $dataArray["productExt"] = $productExt;
        $dataArray["productName"] = $productName;
        $dataArray["serviceType"] = $serviceType;
        $dataArray["amount"] = $amount;
        $dataArray["amount"] = $amount;
        $dataArray["amount"] = $amount;
        //生成签名
        $sign = Sign::productSign($dataArray, $this->key);
        $dataArray["sign"] = $sign;

        $result = json_decode($this->postSend($urlString, $dataArray));
        if ($result->status == "SUCCESS")
            return '{"result":"1","message":"' . $result->qrCode . '"}';
        else
            return '{"result":"0","message":"' . $result->message . '"}';
    }
    public function ebank()
    {
        $urlString = "http://13.67.73.179/v1/api/ebank/pay";

        $interfaceVersion = array_key_exists("interface_version", $_POST) ? $_POST['interface_version'] : "";
        $key = array_key_exists("key", $_POST) ? $_POST['key'] : "";
        $merchantCode = array_key_exists("merchant_code", $_POST) ? $_POST['merchant_code'] : "";
        $amount = array_key_exists("amount", $_POST) ? $_POST['amount'] : "";
        $clientIp = array_key_exists("client_ip", $_POST) ? $_POST['client_ip'] : "";
        $notifyUrl = array_key_exists("notify_url", $_POST) ? $_POST['notify_url'] : "";
        $orderId = array_key_exists("order_id", $_POST) ? $_POST['order_id'] : "";
        $productDesc = array_key_exists("product_desc", $_POST) ? $_POST['product_desc'] : "";
        $productExt = array_key_exists("product_ext", $_POST) ? $_POST['product_ext'] : "";
        $productName = array_key_exists("product_name", $_POST) ? $_POST['product_name'] : "";
        $returnUrl = array_key_exists("return_url", $_POST) ? $_POST['return_url'] : "";
        $openType = array_key_exists("open_type", $_POST) ? $_POST['open_type'] : "";
        $userType = array_key_exists("user_type", $_POST) ? $_POST['user_type'] : "";
        $cardType = array_key_exists("card_type", $_POST) ? $_POST['card_type'] : "";
        $bankCode = array_key_exists("bank_code", $_POST) ? $_POST['bank_code'] : "";

        if ($amount == "" || $clientIp == "" || $notifyUrl == "" || $orderId == "" || $productName == "" || $returnUrl == "")
            return '参数错误';

        $dataArray = [];
        $dataArray ["amount"] = $amount;
        $dataArray["clientIp"] = $clientIp;
        $dataArray["interfaceVersion"] = $this->interfaceVersion;
        $dataArray["merchantCode"] = $this->merchantCode;
        $dataArray["notifyUrl"] = $notifyUrl;
        $dataArray["orderId"] = $orderId;
        if ($productDesc != "")
            $dataArray["productDesc"] = $productDesc;
        if ($productExt != "")
            $dataArray["productExt"] = $productExt;
        $dataArray["productName"] = $productName;
        $dataArray["openType"] = $openType;
        $dataArray["userType"] = $userType;
        $dataArray["cardType"] = $cardType;
        $dataArray["bankCode"] = $bankCode;
        $dataArray["returnUrl"] = $returnUrl;
        //生成签名
        $sign = Sign::productSign($dataArray, $this->key);
        $dataArray["sign"] = $sign;

       
        return $this->postSend($urlString, $dataArray,"text/html");
    }
    // public function barcode()
    // {
    //     $urlString = "http://13.67.73.179/v1/api/barcode/pay";

    //     $interfaceVersion = array_key_exists("interface_version", $_POST) ? $_POST['interface_version'] : "";
    //     $key = array_key_exists("key", $_POST) ? $_POST['key'] : "";
    //     $merchantCode = array_key_exists("merchant_code", $_POST) ? $_POST['merchant_code'] : "";
    //     $amount = array_key_exists("amount", $_POST) ? $_POST['amount'] : "";
    //     $authCode = array_key_exists("auth_code", $_POST) ? $_POST['auth_code'] : "";
    //     $clientIp = array_key_exists("client_ip", $_POST) ? $_POST['client_ip'] : "";
    //     $notifyUrl = array_key_exists("notify_url", $_POST) ? $_POST['notify_url'] : "";
    //     $orderId = array_key_exists("order_id", $_POST) ? $_POST['order_id'] : "";
    //     $productDesc = array_key_exists("product_desc", $_POST) ? $_POST['product_desc'] : "";
    //     $productExt = array_key_exists("product_ext", $_POST) ? $_POST['product_ext'] : "";
    //     $productName = array_key_exists("product_name", $_POST) ? $_POST['product_name'] : "";
    //     $serviceType = array_key_exists("service_type", $_POST) ? $_POST['service_type'] : "";

    //     if ($amount == "" || $clientIp == "" || $notifyUrl == "" || $orderId == "" || $productName == "" || $serviceType == "" || $authCode == "")
    //         return '{"result":"0","message":"参数错误"}';

    //     $dataArray = [];
    //     $dataArray["amount"] = $amount;
    //     $dataArray["authCode"] = $authCode;
    //     $dataArray["clientIp"] = $clientIp;
    //     $dataArray["interfaceVersion"] = $this->interfaceVersion;
    //     $dataArray["merchantCode"] = $this->merchantCode;
    //     $dataArray["notifyUrl"] = $notifyUrl;
    //     $dataArray["orderId"] = $orderId;
    //     if ($productDesc != "")
    //         $dataArray["productDesc"] = $productDesc;
    //     if ($productExt != "")
    //         $dataArray["productExt"] = $productExt;
    //     $dataArray["productName"] = $productName;
    //     $dataArray["serviceType"] = $serviceType;
    //     $dataArray["amount"] = $amount;
    //     $dataArray["amount"] = $amount;
    //     $dataArray["amount"] = $amount;
    //     //生成签名
    //     $sign = Sign::productSign($dataArray, $this->key);
    //     $dataArray["sign"] = $sign;

    //     $result = json_decode($this->postSend($urlString, $dataArray));
    //     if ($result->status == "FAILED")
    //         return '{"result":"0","message":"' . $result->message . '"}';
    //     else
    //         return '{"result":"1","message":"' . $result->message . '"}';
    // }

    private function postSend($urlString, $dataArray, $acceptString = "application/json")
    {
        $headers = array("Content-type: application/json;charset='utf-8'",
            "Accept: $acceptString",
            "Cache-Control: no-cache",
            "Pragma: no-cache"
        );
        $dataJsonString = json_encode($dataArray);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $urlString);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ($dataJsonString));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
}